﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;
using BEGDC.CC.Entity;
using BEGDC.CC.BL.Workflow;

namespace BEGDC.CC.Web.SystemManagement
{
	/// <summary>
	/// Summary description for ShowTypeForm.
	/// </summary>
	public class ShowLegalEntityForm : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lblTitle;
		protected System.Web.UI.WebControls.Label lbName;
		protected System.Web.UI.WebControls.TextBox txtName;
		protected System.Web.UI.WebControls.TextBox txtDescription;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.Label lbDescription;
		protected System.Web.UI.WebControls.CheckBox chkEnabled;
		protected System.Web.UI.WebControls.Button btnClose;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			if(!Page.IsPostBack)
			{
				InitializeGlobalization();
				string id = Request.QueryString["id"];
				BindLegalEntity(Convert.ToInt32(id));
                this.Page.Response.Expires = 0;

			}
		}

		private void BindLegalEntity(int id)
		{
			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
			BEGDC.CC.Entity.LegalEntity dic = utility.GetLegalEntity(id);

			if(dic != null)
			{
				this.txtName.Text = dic.LegalEntityName;
				this.txtDescription.Text = dic.Description;
				this.chkEnabled.Checked = dic.IsEnabled;
			}
		}

		private void InitializeGlobalization()
		{
			//this.lblTitle.Text = "Modify Legal Entity";
			//this.lbName.Text = "Legal Entity Name";
			//this.lbDescription.Text = "Description";
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.btnSave.Click += new EventHandler(btnSave_Click);
            //this.btnClose.Click += new EventHandler(btnClose_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

        void btnSave_Click(object sender, EventArgs e)
		{
			IUtility utility = AppContainer.Instance[ServiceConst.UtilityService] as IUtility;
			BEGDC.CC.Entity.LegalEntity dic = new BEGDC.CC.Entity.LegalEntity();

			dic.Id = Convert.ToInt32(Request.QueryString["id"]);
			dic.LegalEntityName = this.txtName.Text.Trim();
			
			dic.Description = this.txtDescription.Text;
			dic.IsEnabled = this.chkEnabled.Checked;

			if(utility.SaveLegalEntity(dic))
			{
				Page.RegisterClientScriptBlock("","<script>VBAlert('" + BEGDC.CC.Resource.SystemMessage.CM001 + "');returnValue='Type';window.close();</script>");
				CCIdentity id = this.User.Identity as CCIdentity;
				BEGDC.CC.Entity.OperationLog log = new BEGDC.CC.Entity.OperationLog();
				log.Module = "LegalEntity";
				Employee emp = new Employee();
				emp.Id = id.UserInfo.Id;
				log.Employee = emp;
				log.Time = System.DateTime.Now;
				log.Description = "Update data legal entity.";
				utility.SaveOperationLog(log);
			}
			else
				Page.RegisterClientScriptBlock("","<script>alert('" + BEGDC.CC.Resource.SystemMessage.CM002 + "');</script>");
			
		}

        void btnClose_Click(object sender, EventArgs e)
		{
			Page.RegisterClientScriptBlock("","<script>returnValue='';window.close();</script>");
		}
	}
}
